﻿using System;
using System.IO;

namespace WowProjectPackage.Tests.Import
{
	/// <summary>
	/// Common parts of importer tests.
	/// </summary>
	public class ImporterBaseTest
	{
		#region Protected members

		protected Uri emptyUri;
		protected Uri nonExistingUri;
		protected Uri plainUri;
		protected Uri recursiveUri;
		protected Uri realisticUri;
		protected Uri targetUri;
		protected Uri inFlightUri;
		protected Uri shamanWarningUri;
		protected Uri chinchillaUri;
		protected Uri ace3Uri;
		protected Uri abakusUri;
		protected Uri deadlyBossUri;
		protected Uri outfitterUri;
		protected Uri swStatsUri;

		#endregion

		/// <summary>
		/// Sets up.
		/// </summary>
		public virtual void SetUp()
		{
			string currentDir = Environment.CurrentDirectory;
			emptyUri = new Uri(Path.Combine(currentDir, @"Import\TestData\EmptyFolder"));
			nonExistingUri = new Uri(Path.Combine(currentDir, @"Import\TestData\NonExistingDir"));
			plainUri = new Uri(Path.Combine(currentDir, @"Import\TestData\PlainAddon"));
			recursiveUri = new Uri(Path.Combine(currentDir, @"Import\TestData\RecursiveAddon"));
			realisticUri = new Uri(Path.Combine(currentDir, @"Import\TestData\RealisticAddon"));
			targetUri = new Uri(Path.Combine(currentDir, @"Import\TestData\Target"));
			inFlightUri = new Uri("http://files.wowace.com/InFlight/InFlight-r68765.zip");
			shamanWarningUri = new Uri("http://files.wowace.com/ShamanWarning/ShamanWarning-r65770.zip");
			chinchillaUri = new Uri("http://files.wowace.com/Chinchilla/Chinchilla-r68297.zip");
			ace3Uri = new Uri("http://files.wowace.com/Ace3/Ace3-r69511.zip");
			abakusUri = new Uri("http://files.wowace.com/Abakus/Abakus-r64376.zip");
			deadlyBossUri = new Uri(Path.Combine(currentDir, @"Import\TestData\DeadlyBossAddon"));
			outfitterUri = new Uri(Path.Combine(currentDir, @"Import\TestData\OutfitterAddon"));
			swStatsUri = new Uri(Path.Combine(currentDir, @"Import\TestData\SWStats"));

			DeleteDirectories();
			CreateDirectories();
		}

		/// <summary>
		/// Tears down.
		/// </summary>
		public virtual void TearDown()
		{
			DeleteDirectories();
		}

		#region Private member functions

		/// <summary>
		/// Deletes the directories.
		/// </summary>
		private void DeleteDirectories()
		{
			DeleteDirectoryRecursively(nonExistingUri.LocalPath);
			DeleteDirectoryRecursively(targetUri.LocalPath);
		}

		/// <summary>
		/// Creates the directories.
		/// </summary>
		private void CreateDirectories()
		{
			if (!Directory.Exists(targetUri.LocalPath))
				Directory.CreateDirectory(targetUri.LocalPath);
		}

		/// <summary>
		/// Deletes the directory recursively.
		/// </summary>
		/// <param name="directory">The directory.</param>
		private static void DeleteDirectoryRecursively(string directory)
		{
			if (Directory.Exists(directory))
			{
				foreach (var file in Directory.GetFiles(directory))
				{
					File.Delete(file);
				}

				foreach (var subFolder in Directory.GetDirectories(directory))
				{
					DeleteDirectoryRecursively(subFolder);
				}

				Directory.Delete(directory);
			}
		}

		#endregion
	}
}